package com.frank.yinyuetai.Adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.frank.yinyuetai.Bean.VideoBean;
import com.frank.yinyuetai.PlayVideoListener;
import com.frank.yinyuetai.R;

import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * Created by Administrator on 2016/8/19 0019.
 */
public class YueDanDetailRecycleViewAdapter extends RecyclerView.Adapter<YueDanDetailRecycleViewAdapter.YDViewHolder> {
    private List<VideoBean> mList;
    private Context context;
    private PlayVideoListener playVideoListener;

    public YueDanDetailRecycleViewAdapter(List<VideoBean> mList, Context context, PlayVideoListener playVideoListener) {
        this.mList = mList;
        this.context = context;
        this.playVideoListener = playVideoListener;
    }

    @Override
    public YDViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.yuedan_list_fragment_item, null);
        ButterKnife.bind(this, view);
        return new YDViewHolder(view);
    }

    @Override
    public void onBindViewHolder(YDViewHolder holder, int position) {
        final VideoBean videosBean = mList.get(position);
        holder.artistName.setText(videosBean.getArtistName());
        holder.playCount.setText("播放次数：" + videosBean.getTotalViews());
        holder.title.setText(videosBean.getTitle());
        Glide.with(context).load(videosBean.getPosterPic()).placeholder(R.drawable.empty_logo).into(holder.posterImg);
        holder.itemRoot.setOnClickListener(new View.OnClickListener() {
            /*
    private String url;
    private String hdUrl;
    private String uhdUrl;
    private String shdUrl;
             */
            @Override
            public void onClick(View v) {
                if (videosBean.getShdUrl() != null) {
                    playVideoListener.playVideo(videosBean.getShdUrl(), videosBean.getTitle());
                } else {
                    if (videosBean.getUhdUrl() != null) {
                        playVideoListener.playVideo(videosBean.getUhdUrl(), videosBean.getTitle());
                    } else {
                        if (videosBean.getHdUrl() != null) {
                            playVideoListener.playVideo(videosBean.getHdUrl(), videosBean.getTitle());
                        } else {
                            playVideoListener.playVideo(videosBean.getUrl(), videosBean.getTitle());
                        }
                    }
                }

                Log.i("dsfjlljkl_1", videosBean.getShdUrl() + "::" + videosBean.getTitle());
                Log.i("dsfjlljkl_2", videosBean.getUhdUrl() + "::" + videosBean.getTitle());
                Log.i("dsfjlljkl_3", videosBean.getHdUrl() + "::" + videosBean.getTitle());
                Log.i("dsfjlljkl_4", videosBean.getUrl() + "::" + videosBean.getTitle());
            }
        });

    }

    @Override
    public int getItemCount() {
        return mList.size();
    }

    static class YDViewHolder extends RecyclerView.ViewHolder {
        @Bind(R.id.title)
        TextView title;
        @Bind(R.id.artistName)
        TextView artistName;
        @Bind(R.id.play_count)
        TextView playCount;
        @Bind(R.id.poster_img)
        ImageView posterImg;
        @Bind(R.id.item_root)
        LinearLayout itemRoot;

        public YDViewHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }
}
